package com.drought.rained.presenter;

import com.drought.rained.BuildConfig;
import com.drought.rained.business.CustomConsumerLJ;
import com.drought.rained.business.ParamsFactoryLJ;
import com.drought.rained.cache.UserInfoLJ;
import com.drought.rained.common.ConstantLJ;
import com.drought.rained.mvpview.HomeMvpViewLJ;
import com.drought.rained.response.AnnouncementResponseLJ;
import com.drought.rained.response.OrderItemResponseLJ;
import com.drought.rained.response.ProductResponseLJ;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.functions.Consumer;

public class HomePresenterLJ extends BasePresenterLJ {
    private HomeMvpViewLJ mvpView;

    public HomePresenterLJ(HomeMvpViewLJ mvpView) {
        this.mvpView = mvpView;
    }

    /**
     * 获取产品
     */
    public void getProductData() {
        toSubScribe(req.getProductNew(BuildConfig.channel, UserInfoLJ.getToken(), ParamsFactoryLJ.getCommonParams(null)), new CustomConsumerLJ<ProductResponseLJ>() {
            @Override
            public void call(ProductResponseLJ updateResponse) {
                mvpView.onGetDataNext(updateResponse);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

    /**
     * 获取公告
     */
    public void getAnnouncement() {
        toSubScribe(req.getAnnouncement(BuildConfig.channel, UserInfoLJ.getToken(), ParamsFactoryLJ.getCommonParams(null)), new CustomConsumerLJ<AnnouncementResponseLJ>() {
            @Override
            public void call(AnnouncementResponseLJ updateResponse) {
                mvpView.onGetDataNext(updateResponse);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

    /**
     * 获取订单
     */
    public void getOrderData() {
        Map<String, Object> formMap = new HashMap<>();
        formMap.put("limit", "10"); // 每页10条
        formMap.put("page", "1");
        formMap.put("type", String.valueOf(ConstantLJ.ORDER_STATUS_LENDING)
                + String.valueOf(ConstantLJ.ORDER_STATUS_OVERDUE) + String.valueOf(ConstantLJ.ORDER_STATUS_ROLLOVER));
        toSubScribe(req.getOrderPageList(BuildConfig.channel, UserInfoLJ.getToken(), ParamsFactoryLJ.getCommonParams(formMap)), new CustomConsumerLJ<OrderItemResponseLJ>() {
            @Override
            public void call(OrderItemResponseLJ updateResponse) {
                mvpView.onGetDataNext(updateResponse);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }
}
